﻿using System;
using System.IO;
using System.Net;

namespace SBPweb.PhotoGallery.Common
{

    public class DownloadItem : Uri
    {

        #region Protected fields

        protected Guid seed;

        #endregion

        #region Constuctor logic

        public DownloadItem(Uri uri)
            : base(uri.OriginalString, UriKind.RelativeOrAbsolute)
        {
            seed = Guid.NewGuid();
        }

        public DownloadItem(string uri)
            : base(uri, UriKind.RelativeOrAbsolute)
        {
            seed = Guid.NewGuid();
        }

        public DownloadItem(Uri baseUri, string relativeUri)
            : base(baseUri, relativeUri)
        {
            seed = Guid.NewGuid();
        }

        public DownloadItem(string baseUri, string relativeUri)
            : base(new Uri(baseUri, UriKind.RelativeOrAbsolute), relativeUri)
        {
            seed = Guid.NewGuid();
        }

        public DownloadItem(Uri baseUri, Uri relativeUri)
            : base(baseUri, relativeUri)
        {
            seed = Guid.NewGuid();
        }

        #endregion

        #region Events

        public event EventHandler<DownloadEventArgs> DownloadStarted;

        public event EventHandler<DownloadErrorEventArgs> DownloadError;

        public event EventHandler<DownloadEventArgs> DownloadCancelled;

        public event EventHandler<DownloadProgressEventArgs> DownloadProgressChanged;

        public event EventHandler<DownloadStreamEventArgs> DownloadCompleted;

        #endregion

        #region Methods

        public override bool Equals(object comparand)
        {
            return seed.Equals((comparand as DownloadItem).seed);
        }

        internal void FireOnDownloadStarted()
        {
            OnDownloadStarted(this, new DownloadEventArgs(this));
        }

        internal void FireOnDownloadError(Exception error)
        {
            OnDownloadError(this, new DownloadErrorEventArgs(this, error));
        }

        internal void FireOnDownloadCancelled()
        {
            OnDownloadCancelled(this, new DownloadEventArgs(this));
        }

        internal void FireOnDownloadProgressChanged(DownloadProgressChangedEventArgs e)
        {
            OnDownloadProgressChanged(this, new DownloadProgressEventArgs(this, e));
        }

        internal void FireOnDownloadCompleted(Stream stream)
        {
            OnDownloadCompleted(this, new DownloadStreamEventArgs(this, stream));
        }

        protected virtual void OnDownloadStarted(object sender, DownloadEventArgs e)
        {
            if (DownloadStarted != null)
            {
                DownloadStarted(sender, e);
            }
        }

        protected virtual void OnDownloadError(object sender, DownloadErrorEventArgs e)
        {
            if (DownloadError != null)
            {
                DownloadError(sender, e);
            }
        }

        protected virtual void OnDownloadCancelled(object sender, DownloadEventArgs e)
        {
            if (DownloadCancelled != null)
            {
                DownloadCancelled(sender, e);
            }
        }

        protected virtual void OnDownloadProgressChanged(object sender, DownloadProgressEventArgs e)
        {
            if (DownloadProgressChanged != null)
            {
                DownloadProgressChanged(sender, e);
            }
        }

        protected virtual void OnDownloadCompleted(object sender, DownloadStreamEventArgs e)
        {
            if (DownloadCompleted != null)
            {
                DownloadCompleted(sender, e);
            }
        }

        #endregion

    }

}
